package com.hinnotek.warehouse.ui

import android.graphics.Rect
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.BR
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.bean.ImageViewInfo
import com.hinnotek.warehouse.bean.MachineAccountBean
import com.hinnotek.warehouse.bean.MachineDetailBean
import com.hinnotek.warehouse.bean.MachineListBean
import com.hinnotek.warehouse.databinding.*
import com.hinnotek.warehouse.utils.WAREHOUSE_TYPE_2
import com.hinnotek.warehouse.utils.WAREHOUSE_TYPE_3
import com.hinnotek.warehouse.viewmodel.StockInViewModel
import com.lee.common.adapter.binding.SimpleBindingAdapter
import com.lee.common.base.BaseActivity
import com.lee.common.ext.init
import com.lee.common.observer.DefaultObserver
import com.lee.common.utils.*
import com.xuexiang.xui.widget.imageview.preview.PreviewBuilder
import dagger.hilt.android.AndroidEntryPoint

@Route(path = ACTIVITY_WAREHOUSE_STOCK_OUT_DETAIL)
@AndroidEntryPoint
class StockOutDetailActivity : BaseActivity<ActivityStockOutDetailBinding>() {

    private lateinit var adapter: SimpleBindingAdapter<MachineDetailBean, ItemStockOutDetailLayoutBinding>
    private val viewModel by viewModels<StockInViewModel>()

    var machineAccountBean: MachineAccountBean? = null
    var machineListBean: MachineListBean? = null
    private lateinit var layoutManager: LinearLayoutManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_stock_out_detail)

        setTitle("出库详情列表")
        bindingView.vm = viewModel
        bindingView.executePendingBindings()
        viewModel.attachLoading(loadingState)
        viewModel.dialogMsg.observe(this, DefaultObserver(this))

        machineAccountBean = intent.getSerializableExtra(MACHINE_CONTENT_LIST) as MachineAccountBean
        machineListBean = intent.getSerializableExtra(MACHINE_DETAIL_LIST) as MachineListBean

        bindingView.machineData = machineAccountBean
        bindingView.machineListData = machineListBean
        viewModel.applyDeviceId = machineListBean?.applyDeviceId ?: 0

        viewModel.applyDeviceId = intent.getIntExtra("applyDeviceId", 0)
        setRightText("扫描")?.setOnClickListener {
            val wareId = machineAccountBean?.wareId ?: 0
            val applyDeviceId = machineListBean?.applyDeviceId ?: 0
            Skipping.navScan(
                wareId,
                applyDeviceId,
                WAREHOUSE_TYPE_2,
                this@StockOutDetailActivity
            )
        }

    }

    override fun onResume() {
        viewModel.autoRefresh()
        super.onResume()
    }

    override fun initVew() {
        adapter = SimpleBindingAdapter(
            R.layout.item_stock_out_detail_layout,
            ArrayList(),
            BR.stockInDetail
        )
        bindingView.recycler.init()
        bindingView.recycler.adapter = adapter
        emptyView.adapter = adapter
        viewModel.isEmpty.observe(this, Observer {
            emptyView.values(it)
        })
        emptyView.getErrView().setOnClickListener {
            viewModel.refresh()
        }
        //添加adapter
        adapter.addListener { binding, itemData ->
            binding.QRImage.setOnClickListener {
                PreviewBuilder.from(this)
                    .setImg(ImageViewInfo(itemData.productNumUrl, Rect()))
                    .setFullscreen(true)
                    .setSingleShowType(false)
                    .setProgressColor(R.color.xui_config_color_main_theme)
                    .setType(PreviewBuilder.IndicatorType.Number)
                    .start()
            }
        }
    }

    override fun setData() {
        //加载数据
        viewModel.stockInDetailPage.observe(this, Observer {
            for (machineDetailBean in it) {
                machineDetailBean.applyType = WAREHOUSE_TYPE_2
            }
            adapter.addAll(it, viewModel.isFirst)
        })
    }

}